package com.hrpmar.utils;

import com.hrpmar.controller.dto.EmailDto;
import com.hrpmar.controller.dto.PowerDto;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
//手机jar
import java.io.IOException;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
/**
 * Created by 李松松 on 2017/12/4.
 */
public class EmailUtils {



    //发送邮箱
    public static String SendEmail(EmailDto dto){
        String msg = "";
        // 创建Properties 类用于记录邮箱的一些属性
        final Properties props = System.getProperties();
        //此处填写SMTP服务器
        props.put("mail.smtp.host", "smtp.qq.com");
        // 表示SMTP发送邮件，必须进行身份验证
        props.put("mail.smtp.auth", "true");
        //端口号
        props.put("mail.smtp.port", "587");

        // 此处填写你的账号
        props.put("mail.user", dto.getBEmail());//本人邮箱
        props.put("mail.password", dto.getPwd());//本人密码

        // 设置发件人
        try {
            Authenticator authenticator = new Authenticator() {

                protected PasswordAuthentication getPasswordAuthentication() {
                    // 用户名、密码
                    String userName = props.getProperty("mail.user");
                    String password = props.getProperty("mail.password");
                    return new PasswordAuthentication(userName, password);
                }
            };

            // 使用环境属性和授权信息，创建邮件会话
            Session mailSession = Session.getInstance(props, authenticator);

            // 创建邮件消息
            MimeMessage message = new MimeMessage(mailSession);
            InternetAddress form = new InternetAddress(props.getProperty("mail.user"));//获取自己的邮箱
            message.setFrom(form);
            InternetAddress to = new InternetAddress(dto.getWoEmail());//对方
            message.setRecipient(RecipientType.TO, to);
            message.setSubject(dto.getTheme());//邮箱标题
            message.setContent(dto.getText(), "text/html;charset=UTF-8");//邮箱内容
            Transport.send(message);
            msg="发送成功!";
        } catch (Exception e) {
            // TODO: handle exception
            msg="发送失败!";
        }

    return msg;
    }




    //发送手机短信
    public static void SMSMessage(EmailDto dto) throws IOException {
        HttpClient client = new HttpClient();
        StringBuffer sbf = new StringBuffer();
        sbf.append(dto.getTheme());
        sbf.append(dto.getText());
        PostMethod post = new PostMethod("http://gbk.api.smschinese.cn");
        // PostMethod post = new PostMethod("http://sms.webchinese.cn/web_api/");
        post.addRequestHeader("Content-Type",
                "application/x-www-form-urlencoded;charset=gbk");// 在头文件中设置转码
        NameValuePair[] data = { new NameValuePair("Uid", "lisongsong"),// 注册的用户名
                new NameValuePair("Key", "c78e99e2cf52b3366176"),// 注册成功后，登录网站后得到的密钥
                new NameValuePair("smsMob", dto.getPhone()),// 手机号码
                new NameValuePair("smsText", sbf.toString()) };// 短信内容
        post.setRequestBody(data);

        client.executeMethod(post);
        Header[] headers = post.getResponseHeaders();
        int statusCode = post.getStatusCode();
        System.out.println("statusCode:" + statusCode);
        for (Header h : headers) {
            System.out.println("---" + h.toString());
        }
        String result = new String(post.getResponseBodyAsString().getBytes(
                "gbk"));
        System.out.println(result);
    }


}
